package com.liyi.dao;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;

import com.zero.basic.dao.DefaultBaseDao;
import com.zero.basic.dao.SqlListBaseDao;
import com.zero.basic.dao.SqlQueryBaseDao;
import com.zero.basic.exception.SmsDaoException;
import com.zero.mms.model.Dep;

@Repository("depDaoLiyi")
public class DepDaoImpl extends DefaultBaseDao<Dep> implements DepDao {
	
	@Inject
	private SqlListBaseDao<?> sqlListBaseDao;
	@Inject
	private SqlQueryBaseDao<?> sqlQueryBaseDao;
	
	/* 获取 所有部门 */
	@Override
	public List<Dep> getAllDeps() throws SmsDaoException {
		try {
			String sql = "select * from t_dep";
			List<Dep> deps = sqlListBaseDao.listBySql(sql, Dep.class, true, true);
			return deps;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}
	
	/* 获取 [部门] 及其所有子部门 id */
	@Override
	public List<Integer> getChildDepsByParentId(Integer depId) throws SmsDaoException {
		try {
			String sql = "CALL showChildList(" + depId + ")";
			List<Integer> depIds = sqlListBaseDao.listBySql(sql, null, false, false);
			return depIds;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}

	/* 根据 [部门]id 获取 [部门]名称 */
	@Override
	public String getDepNameByDepId(Integer depId) throws SmsDaoException {
		try {
			String sql = "select dep_name from t_dep where dep_id = " + depId;
			String depName = (String) sqlQueryBaseDao.queryBySql(sql, null, null, false, false);
			return depName;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}

}
